#coding=utf-8
import os
import sys
import urllib
import urllib2
import json
import codecs

sina_url = 'http://news.sina.com.cn/c/gat/2017-12-20/doc-ifypwzxq4208206.shtml'
sina_comment = 'http://comment5.news.sina.com.cn/page/info?&format=json&channel=%s&newsid=comos-%s&page=%d&page_size=%d'

def get_content(url):
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0'
    }
    try:
        req = urllib2.Request(url)
        for key, value in headers.items():
            req.add_header(key, value)
        res = urllib2.urlopen(req)
    except urllib2.HTTPError,e:
        print "The server couldn't fulfill the request"
        print "Error code:",e.code
        print "Return content:",e.read()
        return None
    except urllib2.URLError,e:
        print "Failed to reach the server"
        print "The reason:",e.reason
        return None
    return res

def get_sina_comment(channel, newsid):
    page = 1
    page_size = 100
    all_comments = []
    def get_single_page_comment(page=1, page_size=100):
        sina_comment_url = sina_comment % (channel, newsid, page, page_size)
        res = get_content(sina_comment_url)
        return json.load(res)
    
    i = 1

    data = get_single_page_comment(page, page_size)
    total = data['result']['count']['show']
    page += 1
    total -= page_size
    cmts_list = data['result']['cmntlist']
    for item in cmts_list:
        print i, item['content']
        i+=1
        all_comments.append(item['content'].strip())
    
    while total > 0: 
        data = get_single_page_comment(page, page_size)
        page += 1
        total -= page_size
        cmts_list = data['result']['cmntlist']
        for item in cmts_list:
            print i, item['content']
            i+=1
            all_comments.append(item['content'].strip())
    return all_comments

def help(app_name):
    print '''Usage: %s channel newid
    channel: The channel of news.
    newid: The id of news.
    '''

if __name__ == '__main__':
    if len(sys.argv)>=3:
        channel = sys.argv[1]
        newsid = sys.argv[2]
    else:
        help(sys.argv[0])
        exit()

    f = codecs.open(newsid+'.txt', 'w', 'utf-8')
    all_comments = get_sina_comment(channel, newsid)
    for item in all_comments:
        f.write(item+u'\r\n')
    f.close()
